package com.demo.lifeinfo.common.mine;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.ButtonBarLayout;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.demo.lifeinfo.R;
import com.demo.lifeinfo.app.XBaseActivity;
import com.demo.lifeinfo.entity.User;
import com.demo.lifeinfo.model.GlideApp;
import com.flyco.roundview.RoundTextView;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.google.android.material.appbar.AppBarLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnMultiPurposeListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;

/**
 * created by Raymond
 * on 2019-05-05
 */
public class PersonalnfoAc extends XBaseActivity implements AppBarLayout.OnOffsetChangedListener {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.llt_1)
    LinearLayout llt1;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.relativelayout)
    RelativeLayout relativelayout;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbar_layout)
    AppBarLayout appbarLayout;

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.toolbar_username)
    TextView toolbarUsername;
    @BindView(R.id.buttonBarLayout)
    ButtonBarLayout buttonBarLayout;
    @BindView(R.id.toolbar1)
    Toolbar toolbar1;
    @BindView(R.id.iv_heard)
    ImageView ivHeard;
    @BindView(R.id.head_img)
    ImageView headImg;
    @BindView(R.id.smartLayout)
    SmartRefreshLayout smartLayout;

    User user;
    @BindView(R.id.edit_info)
    RoundTextView editInfo;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    BaseQuickAdapter<String, BaseViewHolder> adapter;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.tv_location)
    TextView tvLocation;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_personal;
    }

    @Override
    protected void initView() {
        user = (User) getIntent().getSerializableExtra("user");
        toolbarUsername.setText(TextUtils.isEmpty(user.getNickeName()) ? "Ta的资料" : user.getNickeName());
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        if (isLogin()) {
            if (user.getObjectId().equals(BmobUser.getCurrentUser(User.class).getObjectId()))
                editInfo.setVisibility(View.VISIBLE);
        }

        appbarLayout.addOnOffsetChangedListener(this);
        smartLayout.setOnMultiPurposeListener(new OnMultiPurposeListener() {
            @Override
            public void onHeaderMoving(RefreshHeader header, boolean isDragging, float percent, int offset, int headerHeight, int maxDragHeight) {
                //设置图片向下移动
                ivHeard.setTranslationY(offset / 2);
                //设置title渐变效果
                toolbar1.setAlpha(1 - Math.min(percent, 1));
                //设置图片宽度变化   当达到指定设置的指定值后  宽度停止  只向下移动
                if (offset <= 100) {
                    ViewGroup.LayoutParams layoutParams = ivHeard.getLayoutParams();
                    layoutParams.width = (ScreenUtils.getScreenWidth() + offset);
                    ((ViewGroup.MarginLayoutParams) layoutParams).setMargins(-(layoutParams.width - ScreenUtils.getScreenWidth()) / 2, -SizeUtils.dp2px(200), 0, 0);
                    ivHeard.requestLayout();
                }
            }

            @Override
            public void onHeaderReleased(RefreshHeader header, int headerHeight, int maxDragHeight) {

            }

            @Override
            public void onHeaderStartAnimator(RefreshHeader header, int headerHeight, int maxDragHeight) {

            }

            @Override
            public void onHeaderFinish(RefreshHeader header, boolean success) {

            }

            @Override
            public void onFooterMoving(RefreshFooter footer, boolean isDragging, float percent, int offset, int footerHeight, int maxDragHeight) {

            }

            @Override
            public void onFooterReleased(RefreshFooter footer, int footerHeight, int maxDragHeight) {

            }

            @Override
            public void onFooterStartAnimator(RefreshFooter footer, int footerHeight, int maxDragHeight) {

            }

            @Override
            public void onFooterFinish(RefreshFooter footer, boolean success) {

            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

            }

            @Override
            public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {

            }
        });

        adapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_hobby) {
            @Override
            protected void convert(BaseViewHolder helper, String item) {
                RoundTextView textView = helper.getView(R.id.item);
                textView.setText(item);

            }
        };
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

    }

    @Override
    protected void initData() {
        setData(user);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isLogin()) {
            if (BmobUser.getCurrentUser(User.class).getObjectId().equals(user.getObjectId())) {
                setData(BmobUser.getCurrentUser(User.class));
            }
        }

    }

    private void setData(User user) {
        GlideApp.with(this)
                .load(user.getAvatar() == null ? R.drawable.ic_launcher_background : user.getAvatar().getFileUrl())
                .centerCrop()
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .circleCrop()
                .into(headImg);
        GlideApp.with(this).load(user.getAvatar() == null ? R.drawable.ic_launcher_background : user.getAvatar().getFileUrl()).centerCrop().into(ivHeard);
        tvName.setText(TextUtils.isEmpty(user.getNickeName()) ? "陌生的Ta" : user.getNickeName());
        tvSex.setText(TextUtils.isEmpty(user.getSex()) ? "未知" : user.getSex());
        tvLocation.setText(TextUtils.isEmpty(user.getCity()) ? "未知" : user.getCity());
        tvMessage.setText(TextUtils.isEmpty(user.getMotto()) ? "此人很懒，没有写入人生标语" : user.getMotto());
        if (user.getHobby() != null) {
            adapter.setNewData(user.getHobby());
        }
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        int scrollRangle = appBarLayout.getTotalScrollRange();
        /**
         * 如果是verticalOffset改成负数   有不一样的效果，可以模拟试试
         */
        ivHeard.setTranslationY(verticalOffset);

        /**
         * 这个数值可以自己定义
         */


        int mAlpha = (int) Math.abs(255f / scrollRangle * verticalOffset);
        //顶部title渐变效果
        toolbar1.setBackgroundColor(Color.argb(mAlpha, 0, 133, 119));
    }


    @OnClick(R.id.edit_info)
    public void onViewClicked() {
        startActivity(new Intent(this, EditPersonalInfoAc.class));
    }


    @OnClick(R.id.btn_publish)
    public void onViewClicked2() {
        Intent it2 = new Intent(this, MyPublishAc.class);
        it2.putExtra("user", user);
        startActivity(it2);
    }


}
